package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVo;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 *RestController注解:
 *  1.组合注解，相当于Controller+ ResponseBody 的组合；
 *  2.在类上添加该注解后 所有控制器方法上无需再添加@ResponseBody注解了
 *
 * @Slf4j
 *   1.Lombok提供的日志注解
 *   2.在类上添加该注解后，会为该类创建一个日志对象log
 */
@Tag(name="01-资讯管理模块", description = "01-资讯管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    /**
     * 添加资讯功能
     */
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("add")
    @Operation(summary = "01-添加资讯")
    @ApiOperationSupport(order = 10)
    public JsonResult add(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam = " + noticeAddParam);
        //将资讯信息插入数据库中
        Notice notice = new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddParam,notice);
        //处理其他属性
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }
    /**
     *资讯列表功能
     */
    @GetMapping("list")
    @Operation(summary = "02-查询资讯")
    @ApiOperationSupport(order = 20)
    public JsonResult list(NoticeAddQuery noticeAddQuery){
        System.out.println("noticeAddQuery = "+ noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();

        List<NoticeListVO> noticeListVOS=noticeMapper.selectNotice(title,type);

        return JsonResult.ok(noticeListVOS);
    }

    /**
     * 资讯详情
     */
    @GetMapping("detail")
    @Operation(summary = "03-资讯详情")
    @ApiOperationSupport(order = 30)
    @Parameters({
            @Parameter(name="id",description = "资讯编号", required = true),
            @Parameter(name="username",description = "用户名", required = true,
                    hidden = true)
    })
    public JsonResult detail(Integer id,String username){
        //username参数只是为了Knife4j测试，没有其他任何作用
        System.out.println("id = " + id);
        NoticeDetailInfoVo noticeDetailInfoVo
                = noticeMapper.SelectNoticeById(id);
        return JsonResult.ok(noticeDetailInfoVo);
    }

    /**
     * 删除资讯
     */
    @DeleteMapping("del")
    @Operation(summary = "04-删除资讯")
    @ApiOperationSupport(order = 40)
    @Parameter(name="id",description = "资讯编号",required = true)
    public JsonResult del(Integer id){
        System.out.println("id = "+ id );
        int i=noticeMapper.deleteById(id);
        if(i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    /**
     * 更新资讯
     */
    @PostMapping("update")
    @Operation(summary = "05-更新资讯")
    @ApiOperationSupport(order = 50)
    @Parameters({
            @Parameter(name = "id"),
            @Parameter(name = "title"),
            @Parameter(name = "content"),
            @Parameter(name = "type"),
            @Parameter(name = "status"),
            @Parameter(name = "noticeUpdateParam",hidden = true),
    })
    public JsonResult update(NoticeUpdateParam noticeUpdateParam){
        System.out.println("noticeUpdateParam = "+ noticeUpdateParam);
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        //处理其他属性
        notice.setUpdateTime(new Date());
        int i=noticeMapper.updateNoticeById(notice);
        if (i>0){
            return JsonResult.ok();

        }
        return new JsonResult(StatusCode.OPERATION_FAILED);

    }



}
